% Quantile Treatment Effects and Bootstrap Inference under Covariate-Adaptive Randomization 
% Yichong Zhang, Xin Zheng
% 2020/02/14

% [Generate q1 in IPW estimation]

function q1 = myqr1(Y,T,P,k,W)
[e,idx] = sort(Y.*T+(10000+Y).*(1-T));
e = [e,W(idx)./(P(idx)+eps)];
L =length(k);
n = length(Y);
k1 = sum(T.*W./(P+eps))*k/n;
q1 = zeros(1,L);
count = 0;
e(:,3) = zeros(n,1);
for i = 2:(floor(max(k1))+1)
    e(i,3) = sum(e(1:i,2));

 for l = 1:L
      if e(i,3)>=k1(l) && e(i-1,3)<k1(l)
        q1(l) = e(i,1);
        count = count+1;
      end
 end
if count == L
    break;
end
end
end

